package service

import (
	"admin100/app/dao"
	"admin100/app/model"
	"admin100/library/define"
	"admin100/library/define/defErr"
	"admin100/library/utils"
	util_math "admin100/library/utils/math"

	"github.com/gogf/gf/frame/g"
)

var Statistics = &statisticsService{}

type statisticsService struct {
}

func (s *statisticsService) FetchDailyStatistics(req *model.DailyStatisticsReq) []*model.DailyStatistics {
	var (
		column = dao.DailyStatistics.Columns
	)
	var channelSlice = utils.TrimSplitStr(req.ChannelList)

	m := dao.DailyStatistics.Fields(
		column.Time,
		"sum("+column.ChargeMoney+") "+column.ChargeMoney,
		"sum("+column.TotalChargeMoney+") "+column.TotalChargeMoney,
		"sum("+column.NewChargeMoney+") "+column.NewChargeMoney,
		"sum("+column.ChargePlayerCount+") "+column.ChargePlayerCount,
		"sum("+column.NewChargePlayerCount+") "+column.NewChargePlayerCount,
		"sum("+column.FirstChargePlayerCount+") "+column.FirstChargePlayerCount,
		"sum("+column.LoginTimes+") "+column.LoginTimes,
		"sum("+column.LoginPlayerCount+") "+column.LoginPlayerCount,
		"sum("+column.ActivePlayerCount+") "+column.ActivePlayerCount,
		"sum("+column.AvgOnlineCount+") "+column.AvgOnlineCount,
		"sum("+column.RegisterCount+") "+column.RegisterCount,
		"sum("+column.CreateRoleCount+") "+column.CreateRoleCount,
		"sum("+column.ValidRoleCount+") "+column.ValidRoleCount,
		"sum("+column.TotalCreateRoleCount+") "+column.TotalCreateRoleCount,
		"sum("+column.ShareCreateRoleCount+") "+column.ShareCreateRoleCount,
		"sum("+column.TotalRegisterCount+") "+column.TotalRegisterCount,
	).WhereValNotEmpty(g.Map{
		column.PlatformId:                req.PlatformId,
		column.ServerId:                  req.ServerId,
		column.Channel:                   channelSlice,
		column.Time + " between ? and ?": g.Slice{req.StartTime, req.EndTime},
	}).Order(column.Time + " asc")

	data, err := m.Group(column.Time).All()
	defErr.IfPanic(err, define.DataOptFail)

	for _, d := range data {
		d.PlatformId = req.PlatformId
		d.ServerId = req.ServerId
		d.Channel = req.ChannelList
		d.ARPU = util_math.CaclRate(d.ChargeMoney, d.ChargePlayerCount)
		d.ActiveARPU = util_math.CaclRate(d.ChargeMoney, d.LoginPlayerCount)
		d.ActiveChargeRate = util_math.CaclRate(d.ChargePlayerCount, d.ActivePlayerCount)
	}
	return data
}

type ServerOnlineStatistics struct {
	PlatformId string `json:"platformId"`
	//ServerId                    string    `json:"serverId"`
	//TodayCreateRole             int                  `json:"todayCreateRole"`
	TodayRegister int `json:"todayRegister"`
	OnlineCount   int `json:"onlineCount"`
	//OnlineIpCount               int                  `json:"onlineIpCount"`
	//MaxOnlineCount              int                  `json:"maxOnlineCount"`
	//AverageOnlineCount          int                  `json:"averageOnlineCount"`
	//TodayOnlineList             [] string            `json:"todayOnlineList"`
	//YesterdayOnlineList         [] string            `json:"yesterdayOnlineList"`
	//BeforeYesterdayOnlineList   [] string            `json:"beforeYesterdayOnlineList"`
	//TodayRegisterList           [] string            `json:"todayRegisterList"`
	//YesterdayRegisterList       [] string            `json:"yesterdayRegisterList"`
	//BeforeYesterdayRegisterList [] string            `json:"beforeYesterdayRegisterList"`
	OnlineData   []map[string]string `json:"onlineData"`
	RegisterData []map[string]string `json:"registerData"`
}

func (s *statisticsService) GetServerOnlineStatistics(platformId, serverId string, channelList []string) *ServerOnlineStatistics {
	var (
		today          = utils.GetTodayZeroTimestamp()
		yestoday       = today - 86400
		beforeYestoday = yestoday - 86400
	)

	todayOnlineList, nowOnline := TenMinuteStatistics.Get24hoursOnlineCount(platformId, serverId, channelList, today)
	yesterdayOnlineList, _ := TenMinuteStatistics.Get24hoursOnlineCount(platformId, serverId, channelList, yestoday)
	beforeYesterdayOnlineList, _ := TenMinuteStatistics.Get24hoursOnlineCount(platformId, serverId, channelList, beforeYestoday)
	todayRegisterList, todayRegister := TenMinuteStatistics.Get24hoursRegisterCount(platformId, serverId, channelList, today)
	yesterdayRegisterList, _ := TenMinuteStatistics.Get24hoursRegisterCount(platformId, serverId, channelList, yestoday)
	beforeYesterdayRegisterList, _ := TenMinuteStatistics.Get24hoursRegisterCount(platformId, serverId, channelList, beforeYestoday)

	onlineData := make([]map[string]string, 0, 144)
	for i := 0; i < 6*24; i = i + 1 {
		m := make(map[string]string, 4)
		m["时间"] = utils.FormatTime(i * 10 * 60)
		m["今日在线"] = utils.IfTrueStr(todayOnlineList[i] == "", "0", todayOnlineList[i])
		m["昨日在线"] = utils.IfTrueStr(yesterdayOnlineList[i] == "", "0", yesterdayOnlineList[i])
		m["前日在线"] = utils.IfTrueStr(beforeYesterdayOnlineList[i] == "", "0", beforeYesterdayOnlineList[i])
		onlineData = append(onlineData, m)
	}

	registerData := make([]map[string]string, 0, 144)
	for i := 0; i < 6*24; i = i + 1 {
		m := make(map[string]string, 4)
		m["时间"] = utils.FormatTime(i * 10 * 60)
		m["今日注册"] = utils.IfTrueStr(todayRegisterList[i] == "", "0", todayRegisterList[i])
		m["昨日注册"] = utils.IfTrueStr(yesterdayRegisterList[i] == "", "0", yesterdayRegisterList[i])
		m["前日注册"] = utils.IfTrueStr(beforeYesterdayRegisterList[i] == "", "0", beforeYesterdayRegisterList[i])
		registerData = append(registerData, m)
	}
	serverOnlineStatistics := &ServerOnlineStatistics{
		PlatformId: platformId,
		//ServerId:                    serverId,
		OnlineCount: nowOnline,
		//TodayCreateRole: GetCreateRoleCountByChannelList(gameDb, serverId, channelList, todayZeroTimestamp, todayZeroTimestamp+86400),
		TodayRegister: todayRegister,
		//MaxOnlineCount:              GetMaxOnlineCount(node),
		//TodayOnlineList:             todayOnlineList,
		//YesterdayOnlineList:         yesterdayOnlineList,
		//BeforeYesterdayOnlineList:   beforeYesterdayOnlineList,
		//TodayRegisterList:           todayRegisterList,
		//YesterdayRegisterList:       yesterdayRegisterList,
		//BeforeYesterdayRegisterList: beforeYesterdayRegisterList,
		OnlineData:   onlineData,
		RegisterData: registerData,
	}

	return serverOnlineStatistics

}
